<?php

namespace app\common\model\Post;

use think\Model;
use traits\model\SoftDelete;

// 帖子评论表
class Comment extends Model
{
    use SoftDelete;

    protected $name = "post_comment";

    protected $autoWriteTimestamp = true;

    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 软删除字段
    protected $deleteTime = 'deletetime';

    public function post()
    {
        return $this->belongsTo('app\common\model\post\Post', 'postid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function business()
    {
        return $this->belongsTo('app\common\model\Business\Business', 'busid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // 查找子评论
    public function sublist($pid)
    {
        //查找子集
        $son = $this
            ->with(['business'])
            ->where(['pid' => $pid])
            ->order(['pid asc', 'level asc', 'id asc'])
            ->select();

        if (empty($son)) {
            return [];
        }

        return $son;
    }

    // 查找子评论id
    public function subIds($pid)
    {
        $son = $this
            ->where(['pid' => $pid])
            ->column('id');

        if (empty($son)) {
            return [];
        }

        return $son;
    }

    protected $append = [
        'createtime_text',
        'like_count',
        'like_list',
        'comment_count',
        'reply_business',
        'accept',
    ];

    public function getCommentCountAttr($value, $data)
    {
        $id = $data['id'];

        $count = $this->where(['pid' => $id])->count();

        return $count ? $count : 0;
    }

    public function getLikeListAttr($value, $data)
    {
        $like = $data['like'];

        if (empty($like)) {
            return [];
        }

        //把字符串转换数组
        return explode(',', $like);
    }

    //点赞数量
    public function getLikeCountAttr($value, $data)
    {
        $like = $data['like'];

        if (empty($like)) {
            return 0;
        }

        //把字符串转换数组
        $arr = explode(',', $like);

        //返回数组的长度
        return count($arr);
    }

    //时间戳
    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime = $data['createtime'];

        if (empty($createtime)) {
            return '';
        }

        return date("Y-m-d H:i", $createtime);
    }

    // 回复人的用户信息
    public function getReplyBusinessAttr($value, $data)
    {
        $replyid = $data['replyid'];

        if(!empty($replyid)){
            $commtent = $this->find($replyid);

            if($commtent){
                $business = model('Business.Business')->find($commtent['busid']);

                return $business;
            }
        }

        return null;
    }

    public function getAcceptAttr($value, $data){
        $id = $data['id'];

        if(!empty($replyid)){
            $post = model('Post.Post')->where(['accept_comment_id' => $id])->find();

            if($post){
                return true;
            }
        }

        return false;
    }
}
